﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using Model;

namespace DAL
{
    public  class packetrecordDal
    {
        /// <summary>
        /// 添加领取记录
        /// </summary>
        /// <param name="record"></param>
        /// <returns></returns>
        public static int Addrecord(ms_packetrecord record)
        {

            string sql = "insert into ms_packetrecord(pr_uid,pr_nickname,pr_merchantid,pr_packet,pr_date) values(@pr_uid,@pr_nickname,@pr_merchantid,@pr_packet,@pr_date)";
            MySqlParameter[] values = new MySqlParameter[6];
            values[0] = new MySqlParameter("pr_uid", record.pr_uid);
            values[1] = new MySqlParameter("pr_nickname", record.pr_nickname);
            values[2] = new MySqlParameter("pr_merchantid", record.pr_merchantid);
            values[3] = new MySqlParameter("pr_packet", record.pr_packet);
            values[4] = new MySqlParameter("pr_date", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 使用红包
        /// </summary>
        /// <param name="pr_id"></param>
        /// <returns></returns>
        public static int updatestatus(string pr_id)
        {
            string sql = "update ms_packetrecord pr_status=2 where pr_id=@pr_id";
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("pr_id", pr_id);
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 通过红包活动ID查活动记录
        /// </summary>
        /// <param name="pr_packetid"></param>
        /// <returns></returns>
        public static List<ms_packetrecord> Querybyid(string pr_packetid)
        {
            string sql = "select * from ms_packetrecord where pr_pactiyid=@pr_pactiyid";
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("pr_pactiyid", pr_packetid);
            List<ms_packetrecord> redlist = new List<ms_packetrecord>();
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            while (rd.Read())
            {
                ms_packetrecord packetrecord = new ms_packetrecord();
                packetrecord.pr_id = rd["pr_id"].ToString();
                packetrecord.pr_uid = rd["pr_uid"].ToString();
                packetrecord.pr_nickname = rd["pr_nickname"].ToString();
                packetrecord.pr_merchantid = rd["pr_merchantid"].ToString();
                packetrecord.pr_packet = rd["pr_packet"].ToString();
                packetrecord.pr_date = rd["pr_date"].ToString();
                packetrecord.pr_status = rd["pr_status"].ToString();
                packetrecord.pr_pactiyid = rd["pr_pactiyid"].ToString();//红包活动ID
                redlist.Add(packetrecord);
            }
            rd.Close();
            return redlist;
        }
    }
}
